﻿using CMS_Tools.Domain;
using CMS_Tools.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace CMS_Tools
{
    public partial class BingTranslator : Form
    {
        TranslatorService.LanguageServiceClient client;

        public BingTranslator()
        {
            InitializeComponent();
        }

        private void BingTranslator_Load(object sender, EventArgs e)
        {
            client = new TranslatorService.LanguageServiceClient();
        }

        private void btn_Translate_Click(object sender, EventArgs e)
        {
            string strTranslatedText = null;
            try
            {
                strTranslatedText = Translate(txtTraslateFrom.Text);
                txtTranslatedText.Text = strTranslatedText;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        


        private void btnTranslateProducts_Click(object sender, EventArgs e)
        {
            List<Product> products = Products.GetAllProducts();

            XmlDocument productsXML = new XmlDocument();
            XmlDeclaration xmlDeclaration = productsXML.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement rootNode = productsXML.CreateElement("Products");
            productsXML.InsertBefore(xmlDeclaration, productsXML.DocumentElement);
            productsXML.AppendChild(rootNode);

            XmlDocument arProductsXML = new XmlDocument();
            XmlDeclaration arXmlDeclaration = arProductsXML.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement arRootNode = arProductsXML.CreateElement("Products");
            arRootNode.SetAttribute("Language", "Arabic");
            arProductsXML.InsertBefore(arXmlDeclaration, arProductsXML.DocumentElement);
            arProductsXML.AppendChild(arRootNode);

            XmlDocument enProductsXML = new XmlDocument();
            XmlDeclaration enXmlDeclaration = enProductsXML.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement enRootNode = enProductsXML.CreateElement("Products");
            enRootNode.SetAttribute("Language", "English");
            enProductsXML.InsertBefore(enXmlDeclaration, enProductsXML.DocumentElement);
            enProductsXML.AppendChild(enRootNode);


            foreach (Product product in products)
            {
                // Create a new <Category> element and add it to the root node
                XmlElement productNode = productsXML.CreateElement("Product");
                XmlElement arProductNode = arProductsXML.CreateElement("Product");
                XmlElement enProductNode = enProductsXML.CreateElement("Product");
                
                productsXML.DocumentElement.AppendChild(productNode);
                arProductsXML.DocumentElement.AppendChild(arProductNode);
                enProductsXML.DocumentElement.AppendChild(enProductNode);


                // Create the required nodes
                //Name -----------------------------------------------------------------------------------------------------
                XmlElement nameNode = productsXML.CreateElement("Name");
                XmlElement arNameNode = arProductsXML.CreateElement("Name");
                XmlElement enNameNode = enProductsXML.CreateElement("Name");

                nameNode.InnerText = product.Name;
                arNameNode.InnerText = product.Name;
                enNameNode.InnerText = product.Name;

                productNode.AppendChild(nameNode);
                arProductNode.AppendChild(arNameNode);
                enProductNode.AppendChild(enNameNode);


                //Localized Name -----------------------------------------------------------------------------------------------------
                XmlElement arLocNameNode = arProductsXML.CreateElement("LocalizedName");
                XmlElement enLocNameNode = enProductsXML.CreateElement("LocalizedName");

                arLocNameNode.InnerText = Translate(product.Name);
                enLocNameNode.InnerText = product.Name;

                arProductNode.AppendChild(arLocNameNode);
                enProductNode.AppendChild(enLocNameNode);


                // Short Description -----------------------------------------------------------------------------------------------------
                XmlElement shortDescriptionNode = productsXML.CreateElement("ShortDescription");
                XmlElement arShortDescriptionNode = arProductsXML.CreateElement("ShortDescription");
                XmlElement enShortDescriptionNode = enProductsXML.CreateElement("ShortDescription");

                shortDescriptionNode.InnerText = product.ShortDescription;
                arShortDescriptionNode.InnerText = Translate(product.ShortDescription);
                enShortDescriptionNode.InnerText = product.ShortDescription;

                productNode.AppendChild(shortDescriptionNode);
                arProductNode.AppendChild(arShortDescriptionNode);
                enProductNode.AppendChild(enShortDescriptionNode);


                // Full description -----------------------------------------------------------------------------------------------------
                XmlElement fullDescriptionNode = productsXML.CreateElement("FullDescription");
                XmlElement arFullDescriptionNode = arProductsXML.CreateElement("FullDescription");
                XmlElement enFullDescriptionNode = enProductsXML.CreateElement("FullDescription");

                fullDescriptionNode.InnerText = product.FullDescription;
                arFullDescriptionNode.InnerText = Translate(product.FullDescription);
                enFullDescriptionNode.InnerText = product.FullDescription;

                productNode.AppendChild(fullDescriptionNode);
                arProductNode.AppendChild(arFullDescriptionNode);
                enProductNode.AppendChild(enFullDescriptionNode);
            }

            productsXML.Save(@"D:\src.xml");
            arProductsXML.Save(@"D:\ar.xml");
            enProductsXML.Save(@"D:\en.xml");
        }




        private string Translate(string word)   //From en to ar
        {
            if (String.IsNullOrEmpty(word)) return word;

            if(client == null)
                client = new TranslatorService.LanguageServiceClient();

            try
            {
                return client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", word, "en", "ar");
            }
            catch { return word; }
        }

    }
    
}
